/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import dao.NguoiDungDao;
import dao.ThanhVienDao;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.TimeZone;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import pojo.Loaithanhvien;
import pojo.Nguoidung;
import pojo.Thanhvien;

/**
 *
 * @author Alvis
 */
@ManagedBean
@RequestScoped
public class Admin_MemberController {

    /** Creates a new instance of IndexMemberController */
    public TimeZone getTimeZone() {
    return TimeZone.getDefault();
}   
    private String search="";
    private Thanhvien[] listThanhVien;
    Thanhvien thanhVienTemp = new Thanhvien();
    Nguoidung nguoiDungTemp = new Nguoidung();

    public Admin_MemberController() {
        
        thanhVienTemp.setNguoidung(new Nguoidung());
        getMemberList();
    }

    public Thanhvien getThanhVienTemp() {
        return thanhVienTemp;
    }

    public void setThanhVienTemp(Thanhvien temp) {
        thanhVienTemp = temp;
    }

    public String getMemberList() {
        Thanhvien[] list;
        
        try {
            ThanhVienDao dao = new ThanhVienDao();
            
              list = dao.getAllMember();
            
            listThanhVien = list;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            list = null;
        }
        return "indexmember";
    }

    public String InsertMember() {

        Nguoidung nguoidung = new Nguoidung();
        Thanhvien thanhvien = new Thanhvien();
        Loaithanhvien loaithanhvien = new Loaithanhvien();
        NguoiDungDao nguoiDungDAO = new NguoiDungDao();
        nguoidung.setHoten(thanhVienTemp.getNguoidung().getHoten());
        nguoidung.setNgaysinh(thanhVienTemp.getNguoidung().getNgaysinh());
        nguoidung.setDiachi(thanhVienTemp.getNguoidung().getDiachi());
        nguoidung.setEmail(thanhVienTemp.getNguoidung().getEmail());
        nguoiDungDAO.Insert(nguoidung);
        ThanhVienDao thanhVienDAO = new ThanhVienDao();
        thanhvien.setNguoidung(nguoidung);
        thanhvien.setLoaithanhvien(loaithanhvien);
        thanhvien.getNguoidung().setManguoidung(nguoidung.getManguoidung());
        thanhvien.getLoaithanhvien().setMaloaithanhvien(1);
        thanhvien.setUsername(thanhVienTemp.getUsername());
        thanhvien.setPassword(thanhVienTemp.getPassword());
        thanhvien.setTinhtrangbannick(false);
        thanhVienDAO.Insert(thanhvien);
        return "indexmember";

    }

    public String getMemberUpdate() {
        ThanhVienDao thanhVienDAO = new ThanhVienDao();
        Thanhvien thanhvien =thanhVienDAO.getMember(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("txtUsername"));
        setThanhVienTemp(thanhVienDAO.getMember(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("txtUsername")));
        Date a= thanhVienTemp.getNguoidung().getNgaysinh();
        return "update";

    }

    public String getMemberDelete() {

        ThanhVienDao thanhVienDAO = new ThanhVienDao();
        setThanhVienTemp(thanhVienDAO.getMember(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("txtUsername")));
        return "deletemember";


    }

    public String UpdateMember() {
        Nguoidung nguoidung = new Nguoidung();
        Thanhvien thanhvien = new Thanhvien();
        Loaithanhvien loaithanhvien = new Loaithanhvien();


        NguoiDungDao nguoiDungDAO = new NguoiDungDao();
        nguoidung.setManguoidung(thanhVienTemp.getNguoidung().getManguoidung());
        nguoidung.setHoten(thanhVienTemp.getNguoidung().getHoten());
        nguoidung.setDiachi(thanhVienTemp.getNguoidung().getDiachi());
        nguoidung.setNgaysinh(thanhVienTemp.getNguoidung().getNgaysinh());
        nguoidung.setEmail(thanhVienTemp.getNguoidung().getEmail());
        nguoiDungDAO.Update(nguoidung, thanhVienTemp.getNguoidung().getManguoidung());
        ThanhVienDao thanhVienDAO = new ThanhVienDao();
        thanhvien.setNguoidung(nguoidung);
        thanhvien.setLoaithanhvien(loaithanhvien);
        thanhvien.getNguoidung().setManguoidung(nguoidung.getManguoidung());
        thanhvien.getLoaithanhvien().setMaloaithanhvien(1);
        thanhvien.setUsername(thanhVienTemp.getUsername());
        thanhvien.setPassword(thanhVienTemp.getPassword());
        thanhvien.setTinhtrangbannick(thanhVienTemp.getTinhtrangbannick());
        thanhVienDAO.Update(thanhvien, thanhVienTemp.getUsername());
        return "indexmember";

    }

    public String DeleteMember() {
        
        NguoiDungDao nguoiDungDAO = new NguoiDungDao();
        ThanhVienDao thanhVienDAO = new ThanhVienDao();
      
        thanhVienDAO.Delete(thanhVienTemp.getUsername());
        nguoiDungDAO.Delete(thanhVienTemp.getNguoidung().getManguoidung());
        return "indexmember";


    }
    public String SearchMember()
    {
        Thanhvien[] list;
        search =FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("searchstring").toString();
        try {
            ThanhVienDao dao = new ThanhVienDao();
            if(search.equals(""))
                list = dao.getAllMember();
            else
                list = dao.searchMember(search); 
            listThanhVien = list;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            list = null;
        }
        return "indexmember";
    }

    /**
     * @return the listThanhVien
     */
    public Thanhvien[] getListThanhVien() {
        return listThanhVien;
    }

    /**
     * @return the Search
     */
    public String getSearch() {
        return search;
    }

    /**
     * @param Search the Search to set
     */
    public void setSearch(String Search) {
        this.search = Search;
    }
}
